import {
	getPkgGroupList,
	getSupplierOption,
	getPkgTypeOption,
	getPkgStateOption,
	getAgentPkgGroupList,
	getPkgOfGroup,
	getPkgsList
} from '@/api/package.js'
import {
	getAllAgentOpts,
	getNextAgentOpts,
	getSupplierOpts,
	getFluxPoolOpts,
	getPackageOpts
} from '@/api/options.js'
import {
	getAgentList,
	getAgentListforCardDistube
} from '@/api/customers.js'
import request from '@/utils/request.js'

var common = {

	/***  封装请求方法  ***/
	sysRequest(url, data, method) {
		let obj = {};
		if (method.toLowerCase() == "get") {
			obj = {
				url: url,
				method: method,
				params: data,
			}
		} else if (method.toLowerCase() == "post") {
			obj = {
				url,
				method,
				data,
			}
		}
		return request(obj)
	},





	optionCallback(res, _this, optionsKey) {
		if (res.data.code == 200) {
			_this.$data[optionsKey] = res.data.data.resultList;
		}
	},
	
	
	optionCallback1(res, _this, optionsKey) {
		if (res.data.code == 200) {
			_this.$data[optionsKey] = res.data.data;
		}
	},
	/****获取所有套餐组****/
	getPkgGroupListCom(_this, optionsKey) {
		getPkgGroupList({}).then(res => {
			this.optionCallback1(res, _this, optionsKey)
		})
	},
	/****获取所有套餐****/
	getPkgsListCom(_this, optionsKey) {
		getPkgsList({}).then(res => {
			this.optionCallback1(res, _this, optionsKey)
		})
	},
	/****获取所有套餐组****/
	// getPkgGroupListCom(_this, optionsKey){
	//     getPkgGroupList({}).then(res => {
	//         this.optionCallback(res, _this, optionsKey)
	//     })
	// },
	/****获取供应商****/
	getSupplierOptsCom(_this, optionsKey) {
		getSupplierOpts({}).then(res => {
			this.optionCallback(res, _this, optionsKey)
		})
	},
	getPackageOptsCom(_this, optionsKey) {
		getPackageOpts({}).then(res => {
			this.optionCallback1(res, _this, optionsKey)
		})
	},
	// /****获取供应商（各地市）****/
	// getBranchSupplierOptionCom(_this, optionsKey){
	//     getBranchSupplierList({}).then(res => {
	//         this.optionCallback(res, _this, optionsKey)           
	//     })        
	// },  
	/****获取套餐类型****/
	getPkgTypeOptionCom(_this, optionsKey) {
		getPkgTypeOption().then(res => {
			this.optionCallback1(res, _this, optionsKey)
		});
	},
	/****获取套餐状态****/
	getPkgStateOptionCom(_this, optionsKey) {
		getPkgStateOption().then(res => {
			this.optionCallback1(res, _this, optionsKey)
		});
	},
	/****获取所有代理商****/
	getAllAgentOptsCom(_this, optionsKey) {
		getAllAgentOpts({}).then(res => {
			this.optionCallback(res, _this, optionsKey)
		});
	},

	/****获取客户列表****/
	getAgentListCom(_this, optionsKey) {
		getAgentList({}).then(res => {
			this.optionCallback(res, _this, optionsKey)
		});
	},

	/** 卡片分销代理商列表 */
	getAgentListforCardDistube(_this, optionsKey) {
		getAgentListforCardDistube({}).then(res => {
			this.optionCallback(res, _this, optionsKey)
		});
	},



	/****获取直属下级代理商****/
	getNextAgentOptsCom(_this, optionsKey) {
		getNextAgentOpts({}).then(res => {
			this.optionCallback(res, _this, optionsKey)
		});
	},
	
	
	/****获取流量池****/
	getFluxPoolOptsCom(_this, optionsKey, data) {
		getFluxPoolOpts(data).then(res => {
			this.optionCallback(res, _this, optionsKey)
		});
	},
	/****获取代理商的套餐组****/
	getAgentPkgGroupListCom(_this, optionsKey) {
		getAgentPkgGroupList({}).then(res => {
			this.optionCallback1(res, _this, optionsKey)
		});
	},
	/****获取某一个套餐组下的所有套餐****/
	getPkgOfGroupCom(_this, optionsKey, data) {
		getPkgOfGroup(data).then(res => {
			this.optionCallback(res, _this, optionsKey)
		});
	},
	/*
	 * 重置翻页组件的当前页为第1页
	 */
	resetCurrentPage(_this, $event, tbpgKey) {
		if ($event) {
			_this.$data[tbpgKey].currentPage = 1;
			var initData = _this.$options.data();
			// console.log(initData[tbpgKey].pageSize)
			_this.$data[tbpgKey].pageSize = initData[tbpgKey].pageSize;
		}
	},
	/*
	 * 组合搜索参数与分页参数
	 */
	getRequestParams(_this, tbpgKey, searchParams) {
		var pgParams = {
			perPageNum: _this.$data[tbpgKey].pageSize,
			pageNo: _this.$data[tbpgKey].currentPage
		}

		var params = JSON.parse(JSON.stringify(searchParams));
		deleteSpaceKey(params);
		var params = Object.assign({}, pgParams, params);
		// console.log(params);
		return params;

		function deleteSpaceKey(json) {
			for (var key in json) {
				if (json[key] === '' || json[key] === null) {
					delete json[key];
				}
				if (json.time) {
					json.startTimeString = json.time[0] + ' 00:00:00';
					json.endTimeString = json.time[1] + ' 23:59:59';
					delete json.time;
				}
			}
		}
	},
	/*
	 * 获取表单的回调
	 */
	listCallback(_this, respose, tablePagination) {
		if (respose.data.code == 200) {
			var result = respose.data.data;
			if (result) {
				_this.$data[tablePagination].tableData = result.resultList;
				_this.$data[tablePagination].total = result.totalNum;
				_this.$data[tablePagination].currentPage = result.pageNo;
				_this.$data[tablePagination].pageSize = result.perPageNum;
			} else {
				_this.$data[tablePagination].tableData = [];
				var initData = _this.$options.data()[tablePagination];
				_this.$data[tablePagination].total = initData.total;
				_this.$data[tablePagination].currentPage = initData.currentPage;
				_this.$data[tablePagination].pageSize = initData.pageSize;
			}
		} else {
			_this.$message.error(respose.data.message);
		}
	},

	showTxtByState(state, obj) {
		return obj[state]
	},
	deleteSpaceKey(json) {
		for (var key in json) {
			if (json[key] === '' || json[key] === null) {
				delete json[key];
			}
			if (json.time) {
				json.startTimeString = json.time[0] + ' 00:00:00';
				json.endTimeString = json.time[1] + ' 23:59:59';
				delete json.time;
			}
		}
	}
}

export default common